Skip to content

Conversation

@bulasevich
Copy link
Contributor

@bulasevich bulasevich commented Mar 24, 2025

This is a follow-up to the recent #24102 patch. It addresses an issue where RelocIterator may receive a nullptr as the relocation table address. This change can also serve as an independent fix for JDK-8352112.

RelocIterator::initialize() and RelocIterator::next() perform decrement/increment operations on an internal relocaction pointer.
If nm->relocation_begin() returns nullptr, this results in undefined behavior, as pointer arithmetic on nullptr is prohibited by the C++ Standard.

Instead of introducing a null-check (which would add overhead in RelocIterator::next(), a performance-sensitive path), we initialize _current with a dummy static variable. This pointer is never dereferenced, so its actual value is not important - it just serves to avoid undefined behavior.

RelocIterator::RelocIterator constructor can initialize _current pointer as well. However, in that place we have an assert to ensure that nullptr value is not allowed, and it seems we do not need to apply dummy value there.

Testing:

The fix has been verified against the failure in JDK-8352112. The issue no longer reproduces with this patch, regardless of whether the original fix from #24102 is applied.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8352426: RelocIterator should correctly handle nullptr address of relocation data (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24203/head:pull/24203
$ git checkout pull/24203

Update a local copy of the PR:
$ git checkout pull/24203
$ git pull https://git.openjdk.org/jdk.git pull/24203/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24203

View PR using the GUI difftool:
$ git pr show -t 24203

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24203.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 24, 2025

👋 Welcome back bulasevich! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Mar 24, 2025

@bulasevich This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8352426: RelocIterator should correctly handle nullptr address of relocation data

Reviewed-by: dlong, vlivanov, kvn

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 122 new commits pushed to the master branch:

  • 2483340: 8352579: Refactor CDS legacy optimization for lambda proxy classes
  • 1397ee5: 8334322: Misleading values of keys in jpackage resource bundle
  • 441bd12: 8352812: remove useless class and function parameter in SuspendThread impl
  • e83cccf: 8352948: Remove leftover runtime_x86_32.cpp after 32-bit x86 removal
  • 5672a93: 8348400: GenShen: assert(ShenandoahHeap::heap()->is_full_gc_in_progress() || (used_regions_size() <= _max_capacity)) failed: Cannot use more than capacity #
  • c2a4fed: 8348907: Stress times out when is executed with ZGC
  • 5392674: 8352766: Problemlist hotspot tier1 tests requiring tools that are not included in static JDK
  • 1d205f5: 8352716: (tz) Update Timezone Data to 2025b
  • a2a64da: 8352588: GenShen: Enabling JFR asserts when getting GCId
  • 79bffe2: 8349361: C2: RShiftL should support all applicable transformations that RShiftI does
  • ... and 112 more: https://git.openjdk.org/jdk/compare/b025d8c2e062210b6148da43f11517666b0b4932...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Mar 24, 2025

@bulasevich The following label will be automatically applied to this pull request:

  • hotspot-compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot-compiler hotspot-compiler-dev@openjdk.org label Mar 24, 2025
@bulasevich bulasevich marked this pull request as ready for review March 24, 2025 22:23
@openjdk openjdk bot added the rfr Pull request is ready for review label Mar 24, 2025
@mlbridge
Copy link

mlbridge bot commented Mar 24, 2025

Webrevs

@dean-long
Copy link
Member

This looks OK, but it is still a work-around because RelocIterator forces us to write loops like this:

   while (iter.next()) {
     iter.reloc()->some_operation();

instead of the more modern range-for loop:
https://en.cppreference.com/w/cpp/language/range-for

@bulasevich
Copy link
Contributor Author

instead of the more modern range-for loop

@dean-long
Let's look at this code. Would it be better with a for loop?

    RelocIterator iter(nm, instruction_address(), next_instruction_address());
    while (iter.next()) {
      if (iter.type() == relocInfo::oop_type) {
        oop* oop_addr = iter.oop_reloc()->oop_addr();
        *oop_addr = cast_to_oop(x);
        break;
      } else if (iter.type() == relocInfo::metadata_type) {
        Metadata** metadata_addr = iter.metadata_reloc()->metadata_addr();
        *metadata_addr = (Metadata*)x;
        break;
      }
    }

@bulasevich
Copy link
Contributor Author

This change addresses the bug reported in JDK-8352426 by ensuring that a nullptr relocation table does not result in undefined behavior.

RelocIterator isn’t a simple iterator - it encapsulates a variety of functions beyond just iteration. Reworking the API to support a range-based for loop would require a significant redesign of its interface and behavior. In my opinion, such a rework is beyond the scope of JDK-8352426. If the goal is to modernize the RelocIterator API, including support for range-based for loops, we should either explicitly reformulate JDK-8352426 to include that broader scope, or better yet, create a separate JBS issue to track that as an independent refactoring. What do you think?

@vnkozlov
Copy link
Contributor

I agree with @bulasevich that rewriting RelocIterator is out of scope of this RFE. And I am not sure that we should rewrite it at all.

@vnkozlov
Copy link
Contributor

I submitted out testing.

Copy link
Member

@dean-long dean-long left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I wasn't try to suggest that we need to rewrite RelocIterator now to support C++ iterators, but that that kind of interface could avoid the "nullptr - 1" issue, because then the iterator's begin() and end() functions would both return nullptr, and iteration would end because begin() == end().

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 25, 2025
Copy link
Contributor

@iwanowww iwanowww left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Copy link
Contributor

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My testing passed.

@bulasevich
Copy link
Contributor Author

Good. Thanks for testing. And thanks all for the review!

@bulasevich
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Mar 27, 2025

Going to push as commit 0bfa636.
Since your change was applied there have been 129 commits pushed to the master branch:

  • b7ffd22: 8352980: Purge infrastructure for FP-to-bits interpreter intrinsics after 32-bit x86 removal
  • 9a87e21: 8352800: [PPC] OpenJDK fails to build on PPC after JDK-8350106
  • 4100dc9: 8350801: Add a code signing hook to the JDK build system
  • 8a40498: 8352678: Opensource few JMenuItem tests
  • 66b5dba: 8350988: Consolidate Identity of self-inverse operations
  • 1007811: 8352897: RISC-V: Change default value for UseConservativeFence
  • 7853415: 8352218: RISC-V: Zvfh requires RVV
  • 2483340: 8352579: Refactor CDS legacy optimization for lambda proxy classes
  • 1397ee5: 8334322: Misleading values of keys in jpackage resource bundle
  • 441bd12: 8352812: remove useless class and function parameter in SuspendThread impl
  • ... and 119 more: https://git.openjdk.org/jdk/compare/b025d8c2e062210b6148da43f11517666b0b4932...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Mar 27, 2025
@openjdk openjdk bot closed this Mar 27, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Mar 27, 2025
@openjdk
Copy link

openjdk bot commented Mar 27, 2025

@bulasevich Pushed as commit 0bfa636.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot-compiler hotspot-compiler-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

4 participants